package com.payment.web.controller.app;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.payment.common.base.AjaxResult;
import com.payment.system.service.IPlanItemNewService;
import com.payment.system.service.IPlanNewService;

@Controller
@RequestMapping("/app/newPlan")
public class AppPlanNewController {
    @Autowired
    private IPlanNewService planService;
    @Autowired
    private IPlanItemNewService planItemNewService;

    /**
     * 生成预览计划
     *
     * @param carIdA        A卡id
     * @param carIdB        B卡id
     * @param cardNumberA   A卡卡号
     * @param cardNumberB   B卡卡号
     * @param cardNameA     A卡银行名称
     * @param cardNameB     B卡银行名称
     * @param planMoeny     还款金额
     * @param dateStr       还款时间数组
     * @param region        落地地区
     * @param merid         商户id
     * @param planType      还款类型  1智能(大小并行额) 2完美还 3智能（小额） 4智能（大额）
     * @param everyNum      每天还款笔数
     * @param acqListJson   通道列表json字符串
     * @param merchantName  商户名称
     * @param merchantPhone 商户手机号
     * @return
     */
    @PostMapping("/createPlanEntrance")
    @ResponseBody
    public AjaxResult createPlanEntrance(String carIdA, String carIdB, String cardNumberA,
                                         String cardNumberB, String cardNameA, String cardNameB, BigDecimal planMoeny,
                                         String dateStr, String region, String merid, Integer planType,
                                         Integer everyNum, String acqListJson, String merchantName,
                                         String merchantPhone) {
        try {
            return planService.createPlanEntrance(carIdA, carIdB, cardNumberA, cardNumberB,
                    cardNameA, cardNameB, planMoeny, dateStr, region, merid,
                    planType, everyNum, acqListJson, merchantName, merchantPhone);
        } catch (ParseException e) {
            e.printStackTrace();
            return AjaxResult.error("时间参数不正确");
        }
    }
    
    
	/**
	 * 生成全额预览计划
	 *
	 * @param carId
	 *            卡id
	 * @param cardNumber
	 *            卡号
	 * @param cardName
	 *            银行名称
	 * @param planMoeny
	 *            还款金额
	 * @param dateStr
	 *            还款时间数组
	 * @param region
	 *            落地地区
	 *            商户id
	 * @param everyNum
	 *            每天还款笔数
	 * @param acqListJson
	 *            通道列表json字符串
	 * @param merchantName
	 *            商户名称
	 * @param merchantPhone
	 *            商户手机号
	 * @return
	 */
	@PostMapping("/createPlanEntranceQ")
	@ResponseBody
	public AjaxResult createPlanEntranceQ(String carId, String cardNumber, String cardName, BigDecimal planMoeny,
			String dateStr, String region, String merId, Integer everyNum, String acqListJson, String merchantName,
			String merchantPhone) {
		return planService.createQuanEPlan(carId, cardNumber, cardName, planMoeny, dateStr, region, merId, everyNum,
				acqListJson, merchantName, merchantPhone);
	}
    
    /**
     * 提交计划并保存
     *
     * @param
     * @return
     */
    @PostMapping("/submitPlan")
    @ResponseBody
    public AjaxResult submitPlan(@RequestParam Map<String, Object> map) {
        String pa = JSONObject.toJSONString(map);
        return planService.submitPlan(JSONObject.parseObject(pa));
    }

    /**
     * 获取还款通道列表
     *
     * @param cardId
     * @return
     */
    @PostMapping("/getPlanAcqList")
    @ResponseBody
    public AjaxResult getPlanAcqList(String cardId, String cardNumber, String type, String bankCode) {
        return planService.getPlanAcqList(cardId, cardNumber, type, bankCode);
    }

    /**
     * 获取信用卡列表
     *
     * @param acqIds
     * @return
     */
    @PostMapping("/getCardList")
    @ResponseBody
    public AjaxResult getCardList(String acqIds, String merchantId, String userId, String cardId) {
        return planService.getCardList(acqIds, merchantId, userId, cardId);
    }

	/**
	 * 获取计划列表
	 *
	 * @return
	 */
	@PostMapping("/getPlanList")
	@ResponseBody
	public AjaxResult getPlanList(String cardNumber, Integer page, Integer pageSize,
			@RequestParam(defaultValue = "-1") Integer type) {
		return planService.getAppPlanList(cardNumber, page * pageSize, pageSize,type);
	}

    /**
     * 获取计划详情列表
     *
     * @return
     */
    @PostMapping("/getPlanItemList")
    @ResponseBody
    public AjaxResult getPlanItemList(String planId) {
        return planItemNewService.getPlanItemList(planId);
    }

    /**
     * 获取计划详情列表
     *
     * @param planId
     * @return
     */
    @PostMapping("/deletePlan")
    @ResponseBody
    public AjaxResult deletePlan(String planId, String payPwd) {
        return planService.deletePlan(planId,payPwd);
    }


}
